package com.esd.sys.controller;

import com.esd.common.pojo.R;
import com.esd.sys.entity.User;
import com.esd.sys.entity.dto.PwdDto;
import com.esd.sys.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/logout")
    public R logout(){
        Subject currentUser = SecurityUtils.getSubject();
        // 退出登录
        currentUser.logout();
        return R.ok("已退出登录！！");
    }

    /**
     * 获取全部用户并分页返回
     */
    @PostMapping("userAll")
    public R getUserAll( @RequestBody Map<String , Object> params){
        return userService.getUserAll(params);
    }

    /**
     * 用户登录
     * @param user 接收用户登录数据
     */
    @PostMapping("/login")
    public R UserLogin(@RequestBody User user){
        return userService.userLogin(user);
    }

    @PostMapping("addUser")
    public R addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    @PostMapping("updateUser")
    public R updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping("updateUserPwd")
    public R updateUserPwd(@RequestBody PwdDto pwdDto){
        return userService.updateUserPwd(pwdDto);
    }

    @DeleteMapping("deleteUser/{userId}")
    public R deleteUser(@PathVariable Long userId){
        return userService.deleteUserById(userId);
    }

    @GetMapping("/getUserInfo")
    public R getUserInfo(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        return R.ok(user);
    }

}
